package com.sailfish.springbootdemo.utils;

import com.sailfish.springbootdemo.pojo.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回结果统一处理的工具
 */
public class ResultUtil {

    /**
     * 成功返回
     *
     * @param object 返回内容
     * @return
     */
    public static Result success(Object object) {
        Result result = new Result();
        result.setCode(200);
        result.setMsgCn("成功");
        result.setMsgEn("Success");
        result.setData(object);
        return result;
    }

    /**
     * 失败返回
     *
     * @param code   状态码
     * @param msgCn  返回消息中文
     * @param msgEn  返回消息英文
     * @param object 返回内容
     * @return
     */
    public static Result error(Integer code, String msgCn, String msgEn, Object object) {
        Result result = new Result();
        result.setCode(code);
        result.setMsgCn(msgCn);
        result.setMsgEn(msgEn);
        result.setData(object);
        return result;
    }

    public static Result errorNoPermission(Object object) {
        Result result = new Result();
        result.setCode(500);
        result.setMsgCn("无权限");
        result.setMsgEn("No Permission");
        result.setData(object);
        return result;
    }


    public static Result tableSuccess(Integer page, Integer pageSize, Integer total, List<?> rows) {
        Result result = new Result();
        result.setCode(200);
        result.setMsgCn("成功");
        result.setMsgEn("Success");

        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("pageSize", pageSize);
        map.put("total", total);
        map.put("rows", rows);

        result.setData(map);
        return result;
    }
    public static Result tablePageSuccess(Integer page, Integer pageSize, Integer total, List<?> rows,List<?> row) {
        Result result = new Result();
        result.setCode(200);
        result.setMsgCn("成功");
        result.setMsgEn("Success");

        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("pageSize", pageSize);
        map.put("total", total);
        map.put("rows", rows);
        map.put("row", row);

        result.setData(map);
        return result;
    }
}
